package com.radroxx.balancelg;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.radroxx.balancelg.balancedata.GetBalance;

/**
 * Created by Rodion on 08.07.13.
 */
public class DBHelper extends SQLiteOpenHelper {
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "balancelgdb";

    private static final String TABLE_NAME = "getbalance";
    public static final String WIDGETID = "widgetid";
    public static final String PROVIDER = "provider";
    public static final String LOGIN = "login";
    public static final String PASSWORD = "password";
    public static final String DATANUMERIC = "cash";
    public static final String UPDATETIME = "updatetime";
    private static final String CREATE_TABLE = "create table " + TABLE_NAME + " ( id integer primary key autoincrement, " + WIDGETID + " INTEGER, "+ PROVIDER + " TEXT, " + LOGIN + " TEXT, " + PASSWORD + " TEXT, "+DATANUMERIC+" TEXT , "+UPDATETIME+" INTEGER)";

    private SQLiteDatabase dbw = null ;

    public DBHelper(Context context) {
        super(context, DB_NAME, null,DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL( "DROP TABLE "+ TABLE_NAME +" ;" );
        sqLiteDatabase.execSQL(CREATE_TABLE);
    }

    private boolean DBW(){
        if( dbw == null )
        {
            dbw = getWritableDatabase();
            if ( dbw == null )
                return false ;
        }
        return true;
    }

    public void Delete( GetBalance gb ){
        if( DBW() )
        {
            if( gb.getId() != null )
            {
                int del = dbw.delete(TABLE_NAME,"id=?",new String[]{String.valueOf(gb.getId())});
            }
        }
    }

    public GetBalance getBalance( int widgetId ){
        if( DBW() )
        {
            Cursor cursor = dbw.query( TABLE_NAME , new String[]{ "id" , WIDGETID , PROVIDER , LOGIN , PASSWORD , DATANUMERIC , UPDATETIME } , WIDGETID+"=?" , new String[]{ String.valueOf(widgetId) } ,null ,null ,null );
            if( cursor != null )
            {
                if (cursor.moveToFirst()) {
                    do {
                        GetBalance gb = GetBalance.createGetBalance( cursor );
                        if( widgetId == gb.getWidgetId() )
                            return gb ;
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        }
        return null;
    }

    public GetBalance[] getBalancesAll(){
        if( DBW() )
        {
            Cursor cursor = dbw.query( TABLE_NAME , new String[]{ "id" , WIDGETID , PROVIDER , LOGIN , PASSWORD , DATANUMERIC , UPDATETIME } , null , null ,null ,null ,null );
            if( cursor != null )
            {
                GetBalance[] gb = new GetBalance[ cursor.getCount() ];
                int i = 0 ;
                if (cursor.moveToFirst()) {
                    do {
                        gb[i++] = GetBalance.createGetBalance( cursor );
                    } while (cursor.moveToNext());
                }
                cursor.close();
                return gb ;
            }
        }
        return null;
    }

    public long Save( GetBalance gb ){
        if(DBW() && gb != null )
        {
            GetBalance dgb = getBalance( gb.getWidgetId() );
            if( dgb != null )
            {
                dgb = GetBalance.CopyGetBalance( dgb , gb );
                long id = dbw.update(TABLE_NAME, GetBalance.createContentValues(dgb) ,"id=?",new String[]{String.valueOf(dgb.getId())} );
                if( 1 == id  ) {
                    return id;
                }
                return -1;
            }
            return dbw.insert( TABLE_NAME , null , GetBalance.createContentValues(gb) );
        }
        return -1;
    }

    @Override
    public void close(){
        if( dbw != null )
            dbw.close();
        dbw = null ;
        super.close();
    }
}